package com.nep.controller;

import com.nep.entity.Supervisor;
import com.nep.io.FileIO;
import com.nep.util.JavafxUtil;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.util.List;

public class NepsRecoverViewController {
    @FXML
    private TextField txt_realName;
    @FXML
    private ComboBox<String> combo_sex;
    @FXML
    private TextField txt_phone;
    @FXML
    private PasswordField txt_newPassword;
    //主舞台
    public static Stage primaryStage;

    @FXML
    public void resetPassword() {
        String realName = txt_realName.getText();
        String sex = combo_sex.getValue();
        String phone = txt_phone.getText();
        String newPassword = txt_newPassword.getText();

        if (realName == null || realName.trim().isEmpty() ||
            sex == null || sex.trim().isEmpty() ||
            phone == null || phone.trim().isEmpty() ||
            newPassword == null || newPassword.trim().isEmpty()) {
            JavafxUtil.showAlert(primaryStage, "警告", "请填写完整信息", "所有字段均为必填项，请补全后再提交", "warn");
            return;
        }

        List<Supervisor> list = (List<Supervisor>) FileIO.readObject("supervisor.txt");
        boolean found = false;
        if (list != null) {
            for (Supervisor s : list) {
                if (s.getRealName().equals(realName) &&
                    s.getSex().equals(sex) &&
                    s.getPhone() != null && s.getPhone().equals(phone)) {
                    s.setPassword(newPassword);
                    FileIO.writeObject("supervisor.txt", list);
                    JavafxUtil.showAlert(primaryStage, "成功", "密码重置成功", "请使用新密码登录", "info");
                    found = true;
                    break;
                }
            }
        }
        if (!found) {
            JavafxUtil.showAlert(primaryStage, "错误", "信息不匹配", "未找到匹配的用户信息，请检查输入", "warn");
        }
    }

    @FXML
    public void back() {
        Stage stage = (Stage) txt_realName.getScene().getWindow();
        stage.close();
    }

    @FXML
    private void initialize() {
        combo_sex.getItems().addAll("男", "女");
    }
} 